package com.playball.lottery.customview;

import java.util.ArrayList;
import java.util.List;

import com.playball.lottery.R;
//import com.playball.lottery.frm.buyhall.JddJCZQActivity;


import com.playball.lottery.frm.buyhall.JcfootballActivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * 竞彩足球比分gridview用到的数据适配器
 * 
 * @author Administrator
 * 
 */
public class JczqBfgridviewAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<PictureBF> pictures;
	/** 该gridview在整个ExpandableListView中的组位置 */
	private int groupPos;
	/** 该gridview在整个ExpandableListView中的组位置中的子位置 */
	private int childPos;
	private List<GroupDTO> groups;
	/** 比分类型，主胜。平。主负 */
	private String BFType;
	/** 上下文内容 */
	private Context cxt;
	/** 临时存储选择的赛事，由JcfootballActivity来回调 */
	private ArrayList<ChildDTO> list_children = new ArrayList<ChildDTO>();

	public JczqBfgridviewAdapter(String[] titles, String[] nums, Context context, List<GroupDTO> appData, int groupPosition, int childPosition, String BFTYPE) {
		super();
		pictures = new ArrayList<PictureBF>();
		inflater = LayoutInflater.from(context);
		if (nums != null) {
			for (int i = 0; i < nums.length; i++) {
				PictureBF picture = new PictureBF(titles[i], nums[i]);
				pictures.add(picture);
			}
		}
		groups = appData;
		groupPos = groupPosition;
		childPos = childPosition;
		BFType = BFTYPE;
		cxt = context;
	}

	// /**
	// * 获取选择
	// *
	// * @return
	// */
	// public static List<List<List<String>>> getseltitleslist() {
	// return childData_seltitleslist;
	// }

	@Override
	public int getCount() {
		if (null != pictures) {
			return pictures.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return pictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolderBF viewHolderbf;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.buy_jczq_childs_bqcgridview_items, null);
			viewHolderbf = new ViewHolderBF();
			// 组件ID
			viewHolderbf.RelativeLayout1 = (LinearLayout) convertView.findViewById(R.id.jczq_bqc_RelativeLayout1);
			viewHolderbf.title = (TextView) convertView.findViewById(R.id.jczq_bqc_cg);
			viewHolderbf.num = (TextView) convertView.findViewById(R.id.jczq_bqc_sp);
			convertView.setTag(viewHolderbf);
		} else {
			viewHolderbf = (ViewHolderBF) convertView.getTag();
		}

		// 赛事按钮点击与否
		// 获得行数据（模型）
		final ChildDTO info = groups.get(groupPos).children.get(childPos);
		// Log.i("zjq",
		// "展示info.getList_gridview_selectstate()= "+groupPos+" ,childPos= "+childPos+","+info.getList_gridview_selectstate().toString());
		if (BFType.equals("bfs")) {
			if (((String) ((info.getList_bfsgridview_selectstate()).get(position))).equals("false")) {
//				viewHolderbf.RelativeLayout1.setBackgroundColor(0xffffffff);
				viewHolderbf.RelativeLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_normal);
				// 改变字体颜色
				viewHolderbf.title.setTextColor(0xff666666);
				viewHolderbf.num.setTextColor(cxt.getResources().getColor(R.color.ui_new_txt_content_selected));
			} else {
//				viewHolderbf.RelativeLayout1.setBackgroundColor(0xffe34b56);
				viewHolderbf.RelativeLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_pressed);
				// 改变字体颜色
				viewHolderbf.title.setTextColor(0xffffffff);
				viewHolderbf.num.setTextColor(0xffffffff);
			}
		}

		if (BFType.equals("bfp")) {
			if (((String) ((info.getList_bfpgridview_selectstate()).get(position))).equals("false")) {
//				viewHolderbf.RelativeLayout1.setBackgroundColor(0xffffffff);
				viewHolderbf.RelativeLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_normal);
				// 改变字体颜色
				viewHolderbf.title.setTextColor(0xff666666);
				viewHolderbf.num.setTextColor(cxt.getResources().getColor(R.color.ui_new_txt_content_selected));
			} else {
//				viewHolderbf.RelativeLayout1.setBackgroundColor(0xffe34b56);
				viewHolderbf.RelativeLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_pressed);
				// 改变字体颜色
				viewHolderbf.title.setTextColor(0xffffffff);
				viewHolderbf.num.setTextColor(0xffffffff);
			}
		}

		if (BFType.equals("bff")) {
			if (((String) ((info.getList_bffgridview_selectstate()).get(position))).equals("false")) {
//				viewHolderbf.RelativeLayout1.setBackgroundColor(0xffffffff);
				viewHolderbf.RelativeLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_normal);
				// 改变字体颜色
				viewHolderbf.title.setTextColor(0xff666666);
				viewHolderbf.num.setTextColor(cxt.getResources().getColor(R.color.ui_new_txt_content_selected));
			} else {
//				viewHolderbf.RelativeLayout1.setBackgroundColor(0xffe34b56);
				viewHolderbf.RelativeLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_pressed);
				// 改变字体颜色
				viewHolderbf.title.setTextColor(0xffffffff);
				viewHolderbf.num.setTextColor(0xffffffff);
			}
		}

		final String title = pictures.get(position).getTitle();

		viewHolderbf.title.setText(title);
		viewHolderbf.num.setText(pictures.get(position).getNumsp());
		// 按钮点击事件
		viewHolderbf.RelativeLayout1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (BFType.equals("bfs")) {
					if (((String) ((info.getList_bfsgridview_selectstate()).get(position))).equals("false")) {
						// 保存按钮选择状态
						List<String> list_grid_btn_lsstate = new ArrayList<String>();
						list_grid_btn_lsstate = info.getList_bfsgridview_selectstate();
						list_grid_btn_lsstate.remove(position);
						list_grid_btn_lsstate.add(position, "true");
						info.setList_bfsgridview_selectstate(list_grid_btn_lsstate);
						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
						List<String> selspslist0 = new ArrayList<String>();
						selspslist0 = info.getList_bfsgridview_selectsp();
						selspslist0.remove(position);
						selspslist0.add(position, pictures.get(position).getNumsp());
						info.setList_bfsgridview_selectsp(selspslist0);

						// 保存按钮内容
						List<String> seltitleslist0 = new ArrayList<String>();
						seltitleslist0 = info.getList_bfsgridview_selectconcent();
						if (seltitleslist0 == null || ((seltitleslist0 != null) && (!seltitleslist0.contains(title)))) {
							seltitleslist0.add(title);
						} else if (seltitleslist0.contains(title)) {
							for (int i = 0; i < seltitleslist0.size(); i++) {
								if (seltitleslist0.get(i).equals(title))
									seltitleslist0.remove(i);
							}
						}
						info.setList_bfsgridview_selectconcent(seltitleslist0);

						// 添加选中项到选中列表中，准备显示到btn
						String str0;
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < seltitleslist0.size(); i++) {
							sb.append(seltitleslist0.get(i) + "  "); // 2个空格
						}
						str0 = sb.toString();
//						System.out.println("BF str0= " + str0);
						info.setBtnbfscontent(str0);
//						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示      ,添加时会删除重复的SP值
//						List<String> selspslist0 = new ArrayList<String>();
//						selspslist0 = info.getList_bfsgridview_selectsp();
//						if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//							selspslist0.add(pictures.get(position).getNumsp());
//						} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//							for (int i = 0; i < selspslist0.size(); i++) {
//								if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//									selspslist0.remove(i);
//							}
//						}
//						info.setList_bfsgridview_selectsp(selspslist0);
					} else {
						// 保存按钮选择状态
						List<String> list_grid_btn_lsstate = new ArrayList<String>();
						list_grid_btn_lsstate = info.getList_bfsgridview_selectstate();
						list_grid_btn_lsstate.remove(position);
						list_grid_btn_lsstate.add(position, "false");
						info.setList_bfsgridview_selectstate(list_grid_btn_lsstate);
						
						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
						List<String> selspslist0 = new ArrayList<String>();
						selspslist0 = info.getList_bfsgridview_selectsp();
						selspslist0.remove(position);
						selspslist0.add(position, "0");
						info.setList_bfsgridview_selectsp(selspslist0);

						// 保存按钮内容
						List<String> seltitleslist1 = new ArrayList<String>();
						seltitleslist1 = info.getList_bfsgridview_selectconcent();
						if (seltitleslist1 == null || ((seltitleslist1 != null) && (!seltitleslist1.contains(title)))) {
							seltitleslist1.add(title);
						} else if (seltitleslist1.contains(title)) {
							for (int i = 0; i < seltitleslist1.size(); i++) {
								if (seltitleslist1.get(i).equals(title))
									seltitleslist1.remove(i);
							}
						}
						info.setList_bfsgridview_selectconcent(seltitleslist1);

						// 添加选中项到选中列表中，准备显示到btn
						String str1;
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < seltitleslist1.size(); i++) {
							sb.append(seltitleslist1.get(i) + "  "); // 2个空格
						}
						str1 = sb.toString();
//						System.out.println("BF str1= " + str1);
						// 反选至一个不选
						if (str1.equals("")) {
							info.setBtnbfscontent("主胜");
						} else {
							info.setBtnbfscontent(str1);
						}
//						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//						List<String> selspslist0 = new ArrayList<String>();
//						selspslist0 = info.getList_bfsgridview_selectsp();
//						if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//							selspslist0.add(pictures.get(position).getNumsp());
//						} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//							for (int i = 0; i < selspslist0.size(); i++) {
//								if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//									selspslist0.remove(i);
//							}
//						}
//						info.setList_bfsgridview_selectsp(selspslist0);
					}
				}

				if (BFType.equals("bfp")) {
					if (((String) ((info.getList_bfpgridview_selectstate()).get(position))).equals("false")) {
						// 保存按钮选择状态
						List<String> list_grid_btn_lsstate = new ArrayList<String>();
						list_grid_btn_lsstate = info.getList_bfpgridview_selectstate();
						list_grid_btn_lsstate.remove(position);
						list_grid_btn_lsstate.add(position, "true");
						info.setList_bfpgridview_selectstate(list_grid_btn_lsstate);
						
						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
						List<String> selspslist0 = new ArrayList<String>();
						selspslist0 = info.getList_bfpgridview_selectsp();
						selspslist0.remove(position);
						selspslist0.add(position, pictures.get(position).getNumsp());
						info.setList_bfpgridview_selectsp(selspslist0);

						// 保存按钮内容
						List<String> seltitleslist0 = new ArrayList<String>();
						seltitleslist0 = info.getList_bfpgridview_selectconcent();
						if (seltitleslist0 == null || ((seltitleslist0 != null) && (!seltitleslist0.contains(title)))) {
							seltitleslist0.add(title);
						} else if (seltitleslist0.contains(title)) {
							for (int i = 0; i < seltitleslist0.size(); i++) {
								if (seltitleslist0.get(i).equals(title))
									seltitleslist0.remove(i);
							}
						}
						info.setList_bfpgridview_selectconcent(seltitleslist0);

						// 添加选中项到选中列表中，准备显示到btn
						String str0;
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < seltitleslist0.size(); i++) {
							sb.append(seltitleslist0.get(i) + "  "); // 2个空格
						}
						str0 = sb.toString();
//						System.out.println("bf str0= " + str0);
						info.setBtnbfpcontent(str0);
//						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示,算法有误
//						List<String> selspslist0 = new ArrayList<String>();
//						selspslist0 = info.getList_bfpgridview_selectsp();
//						if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//							selspslist0.add(pictures.get(position).getNumsp());
//						} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//							for (int i = 0; i < selspslist0.size(); i++) {
//								if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//									selspslist0.remove(i);
//							}
//						}
//						info.setList_bfpgridview_selectsp(selspslist0);
					} else {
						// 保存按钮选择状态
						List<String> list_grid_btn_lsstate = new ArrayList<String>();
						list_grid_btn_lsstate = info.getList_bfpgridview_selectstate();
						list_grid_btn_lsstate.remove(position);
						list_grid_btn_lsstate.add(position, "false");
						info.setList_bfpgridview_selectstate(list_grid_btn_lsstate);
						
						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
						List<String> selspslist0 = new ArrayList<String>();
						selspslist0 = info.getList_bfpgridview_selectsp();
						selspslist0.remove(position);
						selspslist0.add(position, "0");
						info.setList_bfpgridview_selectsp(selspslist0);

						// 保存按钮内容
						List<String> seltitleslist1 = new ArrayList<String>();
						seltitleslist1 = info.getList_bfpgridview_selectconcent();
						if (seltitleslist1 == null || ((seltitleslist1 != null) && (!seltitleslist1.contains(title)))) {
							seltitleslist1.add(title);
						} else if (seltitleslist1.contains(title)) {
							for (int i = 0; i < seltitleslist1.size(); i++) {
								if (seltitleslist1.get(i).equals(title))
									seltitleslist1.remove(i);
							}
						}
						info.setList_bfpgridview_selectconcent(seltitleslist1);

						// 添加选中项到选中列表中，准备显示到btn
						String str1;
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < seltitleslist1.size(); i++) {
							sb.append(seltitleslist1.get(i) + "  "); // 2个空格
						}
						str1 = sb.toString();
//						System.out.println("bf str1= " + str1);
						// 反选至一个不选
						if (str1.equals("")) {
							info.setBtnbfpcontent("平");
						} else {
							info.setBtnbfpcontent(str1);
						}
//						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//						List<String> selspslist0 = new ArrayList<String>();
//						selspslist0 = info.getList_bfpgridview_selectsp();
//						if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//							selspslist0.add(pictures.get(position).getNumsp());
//						} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//							for (int i = 0; i < selspslist0.size(); i++) {
//								if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//									selspslist0.remove(i);
//							}
//						}
//						info.setList_bfpgridview_selectsp(selspslist0);
					}
				}

				if (BFType.equals("bff")) {
					if (((String) ((info.getList_bffgridview_selectstate()).get(position))).equals("false")) {
						// 保存按钮选择状态
						List<String> list_grid_btn_lsstate = new ArrayList<String>();
						list_grid_btn_lsstate = info.getList_bffgridview_selectstate();
						list_grid_btn_lsstate.remove(position);
						list_grid_btn_lsstate.add(position, "true");
						info.setList_bffgridview_selectstate(list_grid_btn_lsstate);
						
						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
						List<String> selspslist0 = new ArrayList<String>();
						selspslist0 = info.getList_bffgridview_selectsp();
						selspslist0.remove(position);
						selspslist0.add(position, pictures.get(position).getNumsp());
						info.setList_bffgridview_selectsp(selspslist0);

						// 保存按钮内容
						List<String> seltitleslist0 = new ArrayList<String>();
						seltitleslist0 = info.getList_bffgridview_selectconcent();
						if (seltitleslist0 == null || ((seltitleslist0 != null) && (!seltitleslist0.contains(title)))) {
							seltitleslist0.add(title);
						} else if (seltitleslist0.contains(title)) {
							for (int i = 0; i < seltitleslist0.size(); i++) {
								if (seltitleslist0.get(i).equals(title))
									seltitleslist0.remove(i);
							}
						}
						info.setList_bffgridview_selectconcent(seltitleslist0);

						// 添加选中项到选中列表中，准备显示到btn
						String str0;
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < seltitleslist0.size(); i++) {
							sb.append(seltitleslist0.get(i) + "  "); // 2个空格
						}
						str0 = sb.toString();
//						System.out.println("bf str0= " + str0);
						info.setBtnbffcontent(str0);
//						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//						List<String> selspslist0 = new ArrayList<String>();
//						selspslist0 = info.getList_bffgridview_selectsp();
//						if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//							selspslist0.add(pictures.get(position).getNumsp());
//						} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//							for (int i = 0; i < selspslist0.size(); i++) {
//								if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//									selspslist0.remove(i);
//							}
//						}
//						info.setList_bffgridview_selectsp(selspslist0);
					} else {
						// 保存按钮选择状态
						List<String> list_grid_btn_lsstate = new ArrayList<String>();
						list_grid_btn_lsstate = info.getList_bffgridview_selectstate();
						list_grid_btn_lsstate.remove(position);
						list_grid_btn_lsstate.add(position, "false");
						info.setList_bffgridview_selectstate(list_grid_btn_lsstate);
						
						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
						List<String> selspslist0 = new ArrayList<String>();
						selspslist0 = info.getList_bffgridview_selectsp();
						selspslist0.remove(position);
						selspslist0.add(position, "0");
						info.setList_bffgridview_selectsp(selspslist0);

						// 保存按钮内容
						List<String> seltitleslist1 = new ArrayList<String>();
						seltitleslist1 = info.getList_bffgridview_selectconcent();
						if (seltitleslist1 == null || ((seltitleslist1 != null) && (!seltitleslist1.contains(title)))) {
							seltitleslist1.add(title);
						} else if (seltitleslist1.contains(title)) {
							for (int i = 0; i < seltitleslist1.size(); i++) {
								if (seltitleslist1.get(i).equals(title))
									seltitleslist1.remove(i);
							}
						}
						info.setList_bffgridview_selectconcent(seltitleslist1);

						// 添加选中项到选中列表中，准备显示到btn
						String str1;
						StringBuffer sb = new StringBuffer();
						for (int i = 0; i < seltitleslist1.size(); i++) {
							sb.append(seltitleslist1.get(i) + "  "); // 2个空格
						}
						str1 = sb.toString();
//						System.out.println("bf str1= " + str1);
						// 反选至一个不选
						if (str1.equals("")) {
							info.setBtnbffcontent("主负");
						} else {
							info.setBtnbffcontent(str1);
						}
//						// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//						List<String> selspslist0 = new ArrayList<String>();
//						selspslist0 = info.getList_bffgridview_selectsp();
//						if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//							selspslist0.add(pictures.get(position).getNumsp());
//						} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//							for (int i = 0; i < selspslist0.size(); i++) {
//								if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//									selspslist0.remove(i);
//							}
//						}
//						info.setList_bffgridview_selectsp(selspslist0);
					}
				}
				// 通知btn显示文字改变
				JcfootballActivity.btnChildNamechange();
				// 存入List
				list_children.add(info);
				// for (ChildDTO child : list_children) {
				// String jc_mid = child.getmId();
				// String str_hteam = child.gethTeam();
				// String str_vteam = child.getvTeam();
				// // String str_BtnBQCcontent = child.getBtnBQCcontent();
				// // Log.d("JczqBqcgridviewAdapter",
				// "list_children str_BtnBQCcontent=" + str_BtnBQCcontent);
				// Log.d("JczqBqcgridviewAdapter", "list_children jc_mid=" +
				// jc_mid);
				// Log.d("JczqBqcgridviewAdapter", "list_children str_hteam=" +
				// str_hteam);
				// Log.d("JczqBqcgridviewAdapter", "list_children str_vteam=" +
				// str_vteam);
				// }
				// Log.v("JczqBfgridviewAdapter ",
				// "list_children list_children.size()= " +
				// list_children.size());
				// 回调initMapData这个方法，转存到全局变量里，选号结束
				// JcfootballActivity.initMapData(list_children,"BF");
				((JcfootballActivity) cxt).initMapData(list_children, "BF");
				// 重新调用getview，不要在此事件直接设置组件的显示
				notifyDataSetChanged();
			}
		});
		return convertView;
	}
}

class ViewHolderBF {
	public TextView title;
	public TextView num;
	public LinearLayout RelativeLayout1;
}

class PictureBF {
	private String title;
	private String numsp;

	public PictureBF() {
		super();
	}

	public PictureBF(String title, String numsp) {
		super();
		this.title = title;
		this.numsp = numsp;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getNumsp() {
		return numsp;
	}

	public void setNumsp(String numsp) {
		this.numsp = numsp;
	}

}
